# coding=utf-8
__author__ = "leslie"
from pymongo import MongoClient

class Mongodb():

    def __init__(self, host, db):
        self.host = host
        self.db = db

    def __conn(self):
        # conn = MongoClient("192.168.159.101", 27017)
        try:
            conn = MongoClient(self.host, 27017)
            self.daname = conn.list_database_names() # 显示所有数据库
            return conn[self.db]
        except Exception as e:
            print(e)


    def select(self, set):
        db = self.__conn()
        my_set = db[set]
        result = my_set.find()
        return result

    def select_one(self, set):
        db = self.__conn()
        my_set = db[set]
        result = my_set.find({'likes':{"$lt":101}})
        return result

    def insert(self, set, context):
        db = self.__conn()
        my_set = db[set]
        # context = [
        #     {"user":"15309215554","passd":"a123456!"},
        #     {"user":"15309215557","passd":"a123456!"},
        #     {"user":"17888888888","passd":"123456"},
        #     {"user":"17888888887","passd":"123456"},
        #     {"user":"15600000000","passd":"123456"},
        #     {"user":"15600000001","passd":"123456"},
        #     {"user":"18900000010","passd":"123456"},
        #     {"user":"18900000090","passd":"123456"},
        #     {"user": "15309215556", "passd": "123456"},
        #     {"user":"15309215555","passd":"a123456!"},
        #     {"user":"17888888889","passd":"123456"}
        # ]
        result = my_set.insert(context)
        return result

    # def
    # my_set = db.hehe
    # # result = my_set.insert({"shenme":"jdhfasd"})
    # for i in my_set.find():
    #     print(i)

if __name__ == '__main__':
    from pymongo import MongoClient
    conn = MongoClient()
    db = conn.leslie
    result = db.col.find({"title": {"$ne": None}}, {"title": 1, "likes": 1, "_id": 0}).sort('likes', -1)
    for i in result:
        print(i)
